package local;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

import finalProject.Stemmer;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instances;
import weka.core.converters.ArffSaver;

public class CheckPair {

	private static Vector<String> featureVec;
	private static String workName = "0";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		if(CreateVector.workingMode == CreateVector.TWENTY_FILES){
			workName = "20files";
		}else if(CreateVector.workingMode == CreateVector.TWENTY_FILES_WITH_INVERSE){
			workName = "20filesWithInverse";
		}else if(CreateVector.workingMode == CreateVector.ALL_FILES){
			workName = "allFiles";
		}else if(CreateVector.workingMode == CreateVector.ALL_FILES_WITH_INVERSE){
			workName = "allFilesWithInverse";
		} else  if (CreateVector.workingMode == CreateVector.LOCAL) {
			workName = "0";
		}
		
		Stemmer stemmer = new Stemmer();
		String first, second;
		String firstTmp = args[0];
		String secondTmp = args[1];
		stemmer.add(firstTmp.toCharArray(), firstTmp.length());
		stemmer.stem();
		first = stemmer.toString();
		stemmer.add(secondTmp.toCharArray(), secondTmp.length());
		stemmer.stem();
		second = stemmer.toString();
		
		String fileName = "testSet";
		createVector();
		
		FastVector fvWekaVector = new FastVector(featureVec.size() + 1);
		for (String string : featureVec) {
			fvWekaVector.addElement(new Attribute(string));
		}
		
		FastVector fvClassVal = new FastVector(2);
		fvClassVal.addElement("false");
		fvClassVal.addElement("true");
		Attribute boolAttr = new Attribute("boolAttr", fvClassVal);
		fvWekaVector.addElement(boolAttr);
		
		Instances isTrainingSet = new Instances("Rel", fvWekaVector, 0);
		isTrainingSet.setClassIndex(featureVec.size());
		
		ArffSaver saver = new ArffSaver();
		
		try {
			saver.setFile(new File(fileName + ".arff"));
			saver.setStructure(isTrainingSet);
			saver.writeBatch();
			
			String words = first + " " + second;
			File file = new File(fileName + ".arff");
			FileOutputStream out = new FileOutputStream(file, true);
			ArrayList<Integer> positions = null;
			for(int i = 0 ; i <= 32 ; i++)
			{				

				FileReader reader = null;
				URL filePath = null;
				BufferedReader in = null;
				String fileNum;
				String type;
				if(i <= 16)
				{
					fileNum = i<10 ? "0"+i : ""+i ;
					type = "unknown";
				}
				else
				{
					fileNum = (i - 17)<10 ? "0"+(i - 17) : ""+(i - 17) ;
					type = "known";
					
				}
				
				if(CreateVector.workingMode == CreateVector.LOCAL)
				{
					reader = new FileReader(workName + "/" + type + "-r-000" + fileNum);
					in = new BufferedReader(reader);
				}
				else
				{
					filePath = new URL("https://s3.amazonaws.com/ahgfinal/result/" + workName + "/Step2/" + type + "-r-000" + fileNum);
					in = new BufferedReader(new InputStreamReader(filePath.openStream()));
				}
				
				
				
				String inputLine;
				boolean ignoreLine = true;
				boolean found = false;
				while ((inputLine = in.readLine()) != null)
				{
					if(inputLine.equals("True") || inputLine.equals("False"))
					{
						
					}
					else if(ignoreLine)
					{
						if(inputLine.equals(words) == true)
						{
							found = true;
						}
						ignoreLine = false;
						if(found)
							out.write("{".getBytes());
						positions = new ArrayList<Integer>();
					}
					else if(inputLine.isEmpty())
					{
						ignoreLine = true;
						if(found)
						{
							Collections.sort(positions);
							for (Integer integer : positions) {
								out.write(("" + integer + " 1,").getBytes());
							}
						
							out.write(("" + (fvWekaVector.size()-1) + " " + "true" + "}").getBytes());

							out.write("\n".getBytes());
							out.close();
							in.close();
							reader.close();
							return;
						}
					}
					else
					{
						int pos = featureVec.indexOf(inputLine);
//						
						if(pos != -1)
							positions.add(new Integer(pos));
					}
				}
				in.close();
				reader.close();
			}
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
	

	}


	private static void createVector() {

		featureVec = new Vector<String>();
		
		try {
			File file = new File(workName + "Vec.txt");
		    BufferedReader br = new BufferedReader(new FileReader(file));
		    String line;
		    while ((line = br.readLine()) != null) {
		       featureVec.add(line);
		    }
		    br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
